'''
Created on 2020年3月3日

@author: Administrator
'''
from pathlib import Path
from flask import json
from common.public import *
from config import *
from orm.model import  db,TransFormRecorder,TransForm
from view.baseview import BaseView

class TransFormRecorderView(BaseView):
    '''收款记录'''
    def __init__(self):
        pass

    @db.atomic()                   
    def update(self, params):
        """
        编辑收款记录
        """
        transFormRecorder = TransFormRecorder.get(TransFormRecorder.id==params["id"])
        transFormRecorder.transform_id = params["transformid"]
        transFormRecorder.collectiondate = params["collectiondate"]
        transFormRecorder.reciver_id = params["reciverid"]
        transFormRecorder.totalprice = params["totalprice"]
        transFormRecorder.loantype = params["loantype"]
        transFormRecorder.memo = params["memo"]
        annex = []
        for item in params["annex"]:
            annex.append(item['name'])
        transFormRecorder.annex = json.dumps(annex)
        transFormRecorder.save()
        return super().resultSet(200, "收款记录编辑成功。", [])
        raise Exception("收款记录编辑失败")
    
    @db.atomic()
    def add(self, params):
        """
        添加
        """
        annex = []
        for item in params["annex"]:
            annex.append(item["name"])
        TransFormRecorder.create(
               transform_id=params["transformid"],
               collectiondate=params["collectiondate"],
               reciver_id=params["reciverid"],
               totalprice=params["totalprice"],
               loantype=params["loantype"],
               memo=params["memo"],
               annex= json.dumps(annex)
        )
        transform = TransForm.get(TransForm.id == params["transformid"])
        # 实收金额
        transform.amountpayin = params["amountpayin"]
        # 尾款 = 应收金额 - 实收金额
        transform.amountend = int(transform.amountreceivables) - int(transform.amountpayin)
        # 重新审核尾款流转单
        # 接收人审核状态
        transform.receiverstatus = AUDIT_STATUS_WAITE
        # 主管审核状态
        transform.supervisorstatus = AUDIT_STATUS_WAITE
        # 财务审核状态
        transform.financestatus = AUDIT_STATUS_WAITE
        # 状态
        transform.status = AUDIT_STATUS_WAITE
        # 流转类型
        transform.formtype = TRANSFORM_TYPE_ENDED

        transform.save()

        return super().resultSet(200, "尾款流转成功", [])
        raise Exception("款流转失败")
    
#     @db.atomic()                   
#     def delRecorderAnnex(self, param):
#         """
#         删除附件
#         """
#         filename = DOWNLOAD_PATH_TRANSFORMRECORDER_ANNEX + '/'+ str(param['mycustomerid']) +"/"+param['removefile']["name"]
#         if Path(filename).exists():
#             # 指定的目录或文件存在
#             os.remove(filename)
#         else:
#             return super().resultSet(210, "文件不存在更新失败", [])
#         return super().resultSet(200, "更新成功", [])
#         raise Exception("更新失败")
    
    @db.atomic()                   
    def delete(self, id):
        """
        删除
        """
        res = TransFormRecorder.delete().where(TransFormRecorder.id==id).execute()
        if res>=0:
            return super().resultSet(200, "删除成功", {})
        return super().resultSet(403, "删除失败", {})
        raise Exception("删除失败")
